<!-- eslint-disable vue/multi-word-component-names -->
<template>
  <div>
    <!-- 头部 -->
    <div class="head">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <div class="divlable" style="margin-bottom: 10px">仓库编号</div>

          <el-input
            placeholder="请输入"
            style="margin-right: 20px; width: 366px"
            v-model="page.like_code"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <div class="divlable" style="margin-bottom: 10px">仓库名称</div>
          <el-input
            placeholder="请输入"
            style="margin-right: 20px; width: 366px"
            v-model="page.like_name"
          ></el-input>
        </el-form-item>
        <el-form-item label-width="300px">
          <div class="divlable" style="margin-bottom: 10px">仓库状态</div>
          <el-select
            placeholder="请选择"
            style="width: 366px"
            v-model="page.status"
          >
            <el-option label="全部" value=" "></el-option>
            <el-option label="停用" value="0"></el-option>
            <el-option label="启用" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="btn">
          <el-button
            round
            style="margin-right: 20px; background: #ffb200"
            @click="clickfn"
            >搜索</el-button
          >

          <el-button round @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 主体部分 -->
    <div class="content">
      <el-button round class="btn1" @click="$router.push('/Addware')"><span>新增仓库</span> </el-button>
      <el-table :data="list" border style="width: 100%">
        <el-table-column type="index" label="序号" width="67">
        </el-table-column>
        <el-table-column prop="code" label="仓库编码" width="149">
        </el-table-column>
        <el-table-column prop="name" label="仓库名称" width="149">
        </el-table-column>
        <el-table-column prop="type" label="仓库类型" width="149">
        </el-table-column>
        <el-table-column prop="location" label="省/市/区" width="199">
        </el-table-column>
        <el-table-column prop="address" label="详细地址" width="149">
        </el-table-column>
        <el-table-column prop="status" label="仓库状态" width="149">
        </el-table-column>
        <el-table-column prop="surface" label="仓库面积m²" width="149">
        </el-table-column>
        <el-table-column prop="includedNum" label="库区数量" width="149">
        </el-table-column>
        <el-table-column prop="personName" label="负责人" width="149">
        </el-table-column>
        <el-table-column prop="phone" label="联系电话" width="149">
        </el-table-column>
        <el-table-column prop="createTime" label="更新时间" width="199">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="179">
          <template slot-scope="scope">
            <el-button @click="handleClick(scope.row)" type="text" size="small"
              >编辑</el-button
            >
            <el-button type="text" size="small" @click="change(scope.row)">{{
              scope.row.status === '启用' ? "停用" : "启用"
            }}</el-button>
            <el-button type="text" size="small" @click="delfn(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页  -->
      <div class="cen">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="1"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="Number(total)"
        >
        </el-pagination>
      </div>
    </div>

  </div>
</template>

<script>
import { getware, delware, editware } from '@/api/Warehouse'
export default {
  data () {
    return {
      dialogVisible: false,
      list: [],
      sta: '',
      name: '',
      total: 0,
      page: {
        size: 10,
        current: 1,
        descs: 'createTime',
        like_code: '',
        like_name: '',
        status: ''
      }
    }
  },
  created () {
    this.getware()
  },
  methods: {
    handleClick (row) {
      console.log(row)
    },
    handleSizeChange (sizes) {
      this.page.size = sizes
      this.getware()
    },
    handleCurrentChange (current) {
      this.page.current = current
      this.getware()
    },
    async getware () {
      const {
        data: { data }
      } = await getware(this.page)

      this.list = data.records
      this.list.forEach(item => {
        if (item.status === 1) {
          item.status = '启用'
        } else { item.status = '停用' }
      })

      this.list.forEach(item => {
        if (item.type === 'CB') {
          item.type = '储备仓'
        } else if (item.type === 'ZZ') { item.type = ' 中转仓' } else if (item.type === 'JG') { item.type = ' 加工仓' } else { item.type = ' 冷藏仓' }
      })
      this.total = data.total

      console.log(data.records)
    },
    clickfn () {
      this.getware()
    },
    reset () {
      this.page = {
        size: 10,
        current: 1,
        descs: 'createTime',
        like_code: '',
        like_name: '',
        status: ''
      }
      this.getware()
    },
    async delfn (id) {
      await this.$confirm('确认要删除仓库嘛?')
      await delware(id)
      this.getware()
      this.$message.success('删除成功')
    },
    btncancel () {
      this.dialogVisible = false
    },
    async change (row) {
      if (row.status === '启用') {
        await this.$confirm(`确定要停用：${row.name} 吗?`, '确认停用', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'

        }).then(async () => {
          await editware({ id: row.id, status: 0 })

          this.getware()

          this.$message({
            type: 'success',
            message: '状态改变成功!'
          })
        })
      } else {
        this.$confirm(`确定要启用：${row.name} 吗?`, '确认启用', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'

        }).then(async () => {
          await editware({ id: row.id, status: 1 })

          this.getware()

          this.$message({
            type: 'success',
            message: '状态改变成功!'
          })
        })
      }

      this.dialogVisible = true
    }

  }

}
</script>

<style lang="less" scoped>
.head {
  height: 127px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
  padding: 30px;
  margin: 20px 25px;
}
.content {
  height: 800px;
  background: #fff;
  border-radius: 12px;
  box-shadow: 0 0 6px 0 rgb(144 142 142 / 17%);
  margin: 20px 25px;
}
.divlable {
  color: #887e7e;
  font-size: 12px;
  font-family: PingFangSC, PingFangSC-Medium;
  font-weight: 500;
}
::v-deep .el-input__inner {
  height: 41px;
  line-height: 40px;
  border: 1px solid #f8f5f5;
  background: #f8f5f5;
}

.btn {
  margin-top: 50px;
  transform: translateX(180px);
  border-radius: 20px;
}
.btn1 {
  margin-left: 30px;
  margin-top: 20px;
  margin-bottom: 20px;
  background: #00be76;
  color: #fff;
  height: 40px;
  // border-radius: 20px;
  border: 0;
  font-size: 14px;
  font-family: PingFangSC, PingFangSC-Medium;
  font-weight: 500;
  text-align: center;

  cursor: pointer;
  outline: 0;
}
.cen {
  width: 30%;
  margin: 1% auto;
}
</style>
